home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk61 / dchecker / getopt.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  3KB  |  91 lines

  1. /*-
  2. **      @(#)getopt.c    2.5 (smail) 9/15/87
  3. -*/
  4.  
  5. /*-
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  *
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23. -*/
  24.  
  25.  
  26. /* LINTLIBRARY */
  27. #define NULL    0
  28. #define EOF     (-1)
  29. #define ERR(s, c)       if(opterr){\
  30.         extern int write();\
  31.         char errbuf[2];\
  32.         errbuf[0] = c; errbuf[1] = '\n';\
  33.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  34.         (void) write(2, s, (unsigned)strlen(s));\
  35.         (void) write(2, errbuf, 2);}
  36.  
  37. extern char    *index();
  38.  
  39. int             opterr = 1;
  40. int             optind = 1;
  41. int             optopt;
  42. char           *optarg;
  43.  
  44. int
  45. getopt(argc, argv, opts)
  46. int             argc;
  47. char          **argv, *opts;
  48. {
  49.     static int      sp = 1;
  50.     register int    c;
  51.     register char  *cp;
  52.  
  53.     if (sp == 1)
  54.         if (optind >= argc ||
  55.                 argv[optind][0] != '-' || argv[optind][1] == '\0')
  56.             return (EOF);
  57.         else if (strcmp(argv[optind], "--") == NULL) {
  58.             optind++;
  59.             return (EOF);
  60.         }
  61.     optopt = c = argv[optind][sp];
  62.     if (c == ':' || (cp = index(opts, c)) == NULL) {
  63.         ERR(": illegal option -- ", c);
  64.         if (argv[optind][++sp] == '\0') {
  65.             optind++;
  66.             sp = 1;
  67.         }
  68.         return ('?');
  69.     }
  70.     if (*++cp == ':') {
  71.         if (argv[optind][sp + 1] != '\0')
  72.             optarg = &argv[optind++][sp + 1];
  73.         else if (++optind >= argc) {
  74.             ERR(": option requires an argument -- ", c);
  75.             sp = 1;
  76.             return ('?');
  77.         }
  78.         else
  79.             optarg = argv[optind++];
  80.         sp = 1;
  81.     }
  82.     else {
  83.         if (argv[optind][++sp] == '\0') {
  84.             sp = 1;
  85.             optind++;
  86.         }
  87.         optarg = NULL;
  88.     }
  89.     return (c);
  90. }
  91.